package pages {
	import navigation.NavigationView;

	import pages.transitions.UpdateTransition;

	import util.TextFieldFactory;

	import org.splink.deepsplink.navigation.IPageNavigationController;
	import org.splink.deepsplink.navigation.IPageNavigationView;
	import org.splink.deepsplink.navigation.PageNavigationFactory;
	import org.splink.deepsplink.page.IPageSupplier;
	import org.splink.deepsplink.page.Page;
	import org.splink.deepsplink.page.params.PageParameter;
	import org.splink.library.tree.Tree;
	import org.splink.library.tween.QTween;
	import org.splink.library.tween.QTweenConst;
	import org.splink.resource.ResourceProvider;
	import org.splink.resource.resourcebundles.resourcebundle.fonts.FontData;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;

	/**
	 * @author Max Kugland
	 */
	public class TestPage extends Page {
		private var _timer : Timer;
		private var _tf : TextField;
		private var _paramsTf : TextField;

		public function TestPage(supplier : IPageSupplier) {
			super(supplier);
			setup();
		}

		private function setup() : void {
			display.alpha = 0;
			var layer : uint = Tree.getNodeById(id, tree.root).layer;
			display.y = 40 * (layer);
			
			var font : FontData = ResourceProvider.instance().getFontDataByType("default");
			display.addChild(new Bitmap(new BitmapData(550, 400, false, 25000+100000 * layer * Math.random())));
			display.addChild(TextFieldFactory.singleline("<b>" + title + "</b>", 12, font));
			
			_paramsTf = TextFieldFactory.singleline("", 12, ResourceProvider.instance().getFontDataByType("default"));
			display.addChild(_paramsTf).x = 250;
			
			createNavigation();
		}

		private function createNavigation() : DisplayObject {
			var navi : IPageNavigationController = PageNavigationFactory.create(id, tree, navigationCommand);
			var view : IPageNavigationView = navi.addView(new NavigationView(["404"]));
			
			display.addChild(view.display);
			view.display.x = 100;
			return view.display;
		}

		override public function onParamsChanged(params : Array) : void {
			new UpdateTransition().update(_paramsTf, function() : void {
				var p : PageParameter = params[0];
				if(p) {
					_paramsTf.htmlText = p.key + " = " + p.value;
				} else {
					_paramsTf.htmlText = "";
				}
			});
		}
		
		/**
		 * @inheritDoc
		 */
		override public function initialize() : void {
			setShowStrategy(new QTween(display).duration(300).add(QTweenConst.ALPHA, 1));
			setHideStrategy(new QTween(display).duration(300).add(QTweenConst.ALPHA, 0));
			
			_tf = TextFieldFactory.singleline("<b>Initializing</b>", 18, ResourceProvider.instance().getFontDataByType("default"));			display.addChild(_tf).y = 16;
			_timer = new Timer(200, 1);
			_timer.addEventListener(TimerEvent.TIMER, onInitialized);
			_timer.start();
		}

		private function onInitialized(event : TimerEvent) : void {
			if(params.length > 0) {
				var p : PageParameter = params[0];
				_paramsTf.htmlText = p.key + " = " + p.value;
			} else {
				_paramsTf.htmlText = "";
			}
			
			_timer.removeEventListener(TimerEvent.TIMER, onInitialized);
			display.removeChild(_tf);
			setInitialized();
		}

		/**
		 * @inheritDoc
		 */
		override public function finalize() : void {
			if(display.contains(_tf)) display.removeChild(_tf);
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER, onInitialized);
		}
	}
}